package pattern.singleton.枚举;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SingletonTest {
    public static void main(String[] args) throws Exception {
        Singleton instance = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance2 == instance);
        writeObject2File();
        readObjectFormFile();
        readObjectFormFile();
        
    }

    public static void readObjectFormFile() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\workfile\\学习\\b.txt"));
        Singleton singleton = (Singleton) ois.readObject();
        System.out.println(singleton.hashCode());
        ois.close();
    }

    public static void writeObject2File() throws Exception{
        Singleton instance = Singleton.INSTANCE;
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\workfile\\学习\\b.txt"));
        oos.writeObject(instance);
        oos.close();
    }
}
